<template>
  <div class="album-container">
    <div class="album-wrapper">
      <!-- 页面标题 -->
      <div class="page-header">
        <h1 class="page-title">相册展示</h1>
      </div>

      <!-- 相册轮播 -->
      <div class="album-carousel-container">
        <el-carousel
          ref="albumCarousel"
          :height="isPhone ? '400px' : '480px'"
          :autoplay="true"
          :interval="4000"
          :pause-on-hover="true"
          indicator-position="outside"
          arrow="hover"
          type="card"
          :card-scale="0.9"
        >
          <el-carousel-item
            v-for="(item, index) in data.allAlbum"
            :key="index + 'al'"
            @click="fullAction(item.id)"
          >
            <el-card 
              class="album-card"
              :body-style="{ padding: '0px' }"
              shadow="hover"
            >
              <div class="card-image-container">
                <el-image
                  :src="item.photo.link"
                  :alt="item.name"
                  class="album-cover"
                  fit="cover"
                  :preview-src-list="[item.photo.link]"
                  :initial-index="0"
                  preview-teleported
                >
                  <template #error>
                    <div class="image-error">
                      <el-icon size="50"><Picture /></el-icon>
                      <p>图片加载失败</p>
                    </div>
                  </template>
                </el-image>
                
                <!-- 相册信息覆盖层 -->
                <div class="album-overlay">
                  <div class="album-info">
                    <el-tag 
                      :type="getTagType(index)"
                      size="large"
                      class="album-type-tag"
                    >
                      {{ item.albumType.name }}
                    </el-tag>
                    
                    <h3 class="album-title">{{ item.name }}</h3>
                    
                    <p class="album-description">{{ item.description }}</p>
                    
                    <div class="album-stats">
                      <el-icon class="stats-icon"><Picture /></el-icon>
                      <span class="photo-count">{{ data.allPhoto[item.id] }} 张照片</span>
                    </div>
                    
                    <el-button 
                      type="primary" 
                      size="large"
                      class="view-album-btn"
                      @click.stop="fullAction(item.id)"
                    >
                      <el-icon class="btn-icon"><View /></el-icon>
                      查看相册
                    </el-button>
                  </div>
                </div>
              </div>
            </el-card>
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>
    
    <!-- 相册详情弹窗 -->
    <el-dialog
      v-model="visible"
      :title="titleInfo?.name || '相册详情'"
      :width="isPhone ? '95%' : '80%'"
      :fullscreen="isPhone"
      class="album-detail-dialog"
      append-to-body
      destroy-on-close
    >
    <template #header="{ close }">
      <div class="dialog-header">
        <div class="header-info">
          <h3 class="dialog-title">{{ titleInfo?.name || '相册详情' }}</h3>
          <div class="header-meta">
            <el-tag type="info" size="small">
              <el-icon><Clock /></el-icon>
              更新时间: {{ titleInfo.updateTime }}
            </el-tag>
            <el-tag type="success" size="small">
              <el-icon><Picture /></el-icon>
              共 {{ imgList?.length || 0 }} 张照片
            </el-tag>
          </div>
        </div>
        <el-button 
          type="danger" 
          :icon="Close" 
          circle 
          @click="close"
          class="close-btn"
        />
      </div>
    </template>
    
    <el-scrollbar :height="isPhone ? '70vh' : '600px'" class="photo-scrollbar">
      <div class="photo-gallery">
        <el-row :gutter="16">
          <el-col 
            v-for="(item, index) in imgList" 
            :key="index + 'il'"
            :xs="12" 
            :sm="8" 
            :md="6" 
            :lg="4"
            class="photo-col"
          >
            <div class="photo-item">
              <el-image
                :src="item.link"
                :alt="`照片 ${index + 1}`"
                class="gallery-image"
                fit="cover"
                :preview-src-list="imgList.map(img => img.link)"
                :initial-index="index"
                preview-teleported
                lazy
              >
                <template #placeholder>
                  <div class="image-placeholder">
                    <el-icon class="loading-icon"><Loading /></el-icon>
                  </div>
                </template>
                <template #error>
                  <div class="image-error">
                    <el-icon><Picture /></el-icon>
                    <p>加载失败</p>
                  </div>
                </template>
              </el-image>
              
              <!-- 图片序号 -->
              <div class="photo-index">{{ index + 1 }}</div>
            </div>
          </el-col>
        </el-row>
        
        <!-- 空状态 -->
        <el-empty 
          v-if="!imgList || imgList.length === 0" 
          description="暂无照片"
          :image-size="200"
        />
      </div>
    </el-scrollbar>
  </el-dialog>
  </div>
</template>

<script setup>
import {
  computed,
  inject,
  onBeforeUnmount,
  onMounted,
  reactive,
  ref,
} from 'vue'
import { 
  Picture, 
  View, 
  Clock, 
  Close, 
  Loading 
} from '@element-plus/icons-vue'
import { getAlbumTypes, getPhotosByAlbumId } from '@/api/album'
import { ElMessage } from 'element-plus'

const data = reactive({
  type: 0,
  keywords: 'null',
  id: 1,
  allAlbum: [],
  allPhoto: {},
  currentDate: {},
})
const visible = ref(false)
const isPhone = inject('isPhone')
const titleInfo = computed(() => data.currentDate.albumByAlbumId)
const imgList = computed(() => data.currentDate.photosByAlbumId)

onMounted(async () => {
  try {
    // 获取相册数据
    const res = await getAlbumTypes({
      type: data.type,
      keywords: data.keywords,
    })
    
    if (res && res.data) {
      data.allAlbum = res.data.allAlbum || []
      data.allPhoto = res.data.allPhoto || {}
      
      if (data.allAlbum.length === 0) {
        ElMessage.info('暂无相册数据')
      }
    }
  } catch (error) {
    console.error('获取相册数据失败:', error)
    ElMessage.error('获取相册数据失败，请稍后重试')
  }

  // 禁用右键菜单等（保留原有功能）
  const actions = ['contextmenu', 'selectstart', 'copy']
  actions.forEach(function (ev) {
    document.addEventListener(ev, function (event) {
      event.preventDefault()
      event.returnValue = false
    })
  })
})

onBeforeUnmount(() => {
  // 清理事件监听器
  const actions = ['contextmenu', 'selectstart', 'copy']
  actions.forEach(function (ev) {
    document.removeEventListener(ev, preventDefaultAction)
  })
})

// 阻止默认行为的函数
function preventDefaultAction(event) {
  event.preventDefault()
  event.returnValue = false
}

// 打开相册详情
function fullAction(val) {
  visible.value = true
  data.id = val
  getDatesId()
}

// 获取相册详情数据
async function getDatesId() {
  try {
    const res = await getPhotosByAlbumId({
      id: data.id,
    })
    
    if (res && res.data) {
      data.currentDate = res.data
    }
  } catch (error) {
    console.error('获取相册详情失败:', error)
    ElMessage.error('获取相册详情失败，请稍后重试')
  }
}

// 获取标签类型
function getTagType(index) {
  const types = ['primary', 'success', 'info', 'warning', 'danger']
  return types[index % types.length]
}

// 格式化日期
function formatDate(dateString) {
  if (!dateString) return '未知时间'
  
  try {
    const date = new Date(dateString)
    return date.toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    })
  } catch (error) {
    return '时间格式错误'
  }
}
</script>

<style>
/* Element Plus 组件样式覆盖 */
.el-overlay {
  z-index: 1000 !important;
}

.el-carousel__container {
  border-radius: 20px;
  overflow: hidden;
}

.el-carousel__item {
  transition: all 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94) !important;
}

.el-carousel__item.is-animating {
  transition: all 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94) !important;
}

.el-carousel__indicator {
  background-color: rgba(255, 255, 255, 0.4) !important;
  border-radius: 12px !important;
  transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1) !important;
  height: 6px !important;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2) !important;
}

.el-carousel__indicator.is-active {
  background-color: #409eff !important;
  width: 35px !important;
  box-shadow: 0 4px 15px rgba(64, 158, 255, 0.4) !important;
  transform: scale(1.1) !important;
}

.el-carousel__indicator:hover {
  background-color: rgba(64, 158, 255, 0.6) !important;
  transform: scale(1.05) !important;
}

.el-dialog {
  border-radius: 20px;
  overflow: hidden;
}

.el-dialog__header {
  padding: 0 !important;
}

.el-dialog__body {
  padding: 20px !important;
}

.el-image__error {
  background-color: #f5f7fa;
  color: #909399;
  font-size: 14px;
}

.el-tag {
  border-radius: 20px;
  font-weight: 500;
}

.el-button {
  border-radius: 25px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}
</style>

<style scoped>
/* 根容器样式 */
.album-container {
  width: 100%;
  height: 100%;
}

/* 主容器样式 */
.album-wrapper {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 80px 15px 40px 15px; /* 增加顶部间距避免被导航栏遮挡 */
  font-family: 'Helvetica Neue', Arial, sans-serif;
  position: relative;
  overflow-x: hidden;
  box-sizing: border-box;
}

.album-wrapper::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 20% 80%, rgba(64, 158, 255, 0.3) 0%, transparent 50%),
              radial-gradient(circle at 80% 20%, rgba(51, 126, 204, 0.3) 0%, transparent 50%);
  pointer-events: none;
}

/* 页面标题样式 */
.page-header {
  text-align: center;
  margin-bottom: 30px;
  color: #2c3e50;
}

.page-title {
  font-size: 2.8rem;
  font-weight: 700;
  margin: 0 0 10px 0;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  letter-spacing: -1px;
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin: 0;
  font-weight: 300;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2.5rem;
  }
  
  .page-subtitle {
    font-size: 1rem;
  }
}

/* 轮播容器样式 */
.album-carousel-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 相册卡片样式 */
.album-card {
  height: 100%;
  border-radius: 20px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.5s cubic-bezier(0.25, 0.8, 0.25, 1);
  box-shadow: 0 8px 30px rgba(64, 158, 255, 0.15), 0 4px 15px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(64, 158, 255, 0.1);
  position: relative;
}

.album-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, rgba(64, 158, 255, 0.05) 0%, transparent 50%);
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
  z-index: 1;
}

.album-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 20px 40px rgba(64, 158, 255, 0.2), 0 12px 25px rgba(0, 0, 0, 0.1);
  border-color: rgba(64, 158, 255, 0.3);
}

.album-card:hover::before {
  opacity: 1;
}

.card-image-container {
  position: relative;
  height: 100%;
  overflow: hidden;
}

.album-cover {
  width: 100%;
  height: 100%;
  transition: transform 0.4s ease-out;
}

.album-card:hover .album-cover {
  transform: scale(1.05);
}

/* 相册信息覆盖层 */
.album-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.1) 0%,
    rgba(0, 0, 0, 0.3) 50%,
    rgba(0, 0, 0, 0.8) 100%
  );
  display: flex;
  align-items: flex-end;
  padding: 25px;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}

.album-card:hover .album-overlay {
  opacity: 1;
}

.album-info {
  color: white;
  width: 100%;
}

.album-type-tag {
  margin-bottom: 15px;
  backdrop-filter: blur(10px);
  background-color: rgba(255, 255, 255, 0.2) !important;
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white !important;
}

.album-title {
  font-size: 1.8rem;
  font-weight: 600;
  margin: 0 0 15px 0;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
  line-height: 1.2;
}

.album-description {
  font-size: 1rem;
  line-height: 1.5;
  margin: 0 0 20px 0;
  opacity: 0.9;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.album-stats {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
  font-size: 0.9rem;
  opacity: 0.8;
}

.stats-icon {
  margin-right: 8px;
}

.view-album-btn {
  backdrop-filter: blur(15px);
  background: linear-gradient(135deg, #409eff 0%, #337ecc 100%) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  color: white !important;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(64, 158, 255, 0.3);
  position: relative;
  overflow: hidden;
}

.view-album-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s ease;
}

.view-album-btn:hover {
  background: linear-gradient(135deg, #337ecc 0%, #409eff 100%) !important;
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.35);
}

.view-album-btn:hover::before {
  left: 100%;
}

.btn-icon {
  margin-right: 8px;
}

/* 弹窗样式 */
.album-detail-dialog {
  border-radius: 20px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 30px 30px 20px 30px;
  background: linear-gradient(135deg, #409eff 0%, #337ecc 100%);
  color: white;
  position: relative;
  overflow: hidden;
}

.dialog-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 30% 70%, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
  pointer-events: none;
}

.header-info {
  flex: 1;
}

.dialog-title {
  font-size: 1.5rem;
  font-weight: 600;
  margin: 0 0 15px 0;
}

.header-meta {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.header-meta .el-tag {
  background-color: rgba(255, 255, 255, 0.2) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  color: white !important;
}

.close-btn {
  background-color: rgba(255, 255, 255, 0.2) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  color: white !important;
}

.close-btn:hover {
  background-color: rgba(255, 255, 255, 0.3) !important;
}

/* 照片画廊样式 */
.photo-gallery {
  padding: 20px 0;
}

.photo-col {
  margin-bottom: 20px;
}

.photo-item {
  position: relative;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.1), 0 3px 10px rgba(0, 0, 0, 0.05);
  transition: all 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
  border: 1px solid rgba(64, 158, 255, 0.05);
}

.photo-item:hover {
  transform: translateY(-5px) scale(1.01);
  box-shadow: 0 15px 30px rgba(64, 158, 255, 0.15), 0 8px 20px rgba(0, 0, 0, 0.08);
  border-color: rgba(64, 158, 255, 0.2);
}

.gallery-image {
  width: 100%;
  height: 200px;
  border-radius: 15px;
  transition: transform 0.25s ease-out;
}

.photo-item:hover .gallery-image {
  transform: scale(1.03);
}

.photo-index {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 5px 10px;
  border-radius: 15px;
  font-size: 0.8rem;
  font-weight: 500;
  backdrop-filter: blur(10px);
}

/* 图片加载状态 */
.image-placeholder,
.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 200px;
  background-color: #f5f7fa;
  color: #909399;
  border-radius: 15px;
}

.loading-icon {
  animation: rotate 2s linear infinite, pulse 1.5s ease-in-out infinite alternate;
  color: #409eff;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  from {
    opacity: 0.6;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1.1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .album-wrapper {
    padding: 70px 8px 30px 8px; /* 移动端也需要顶部间距 */
  }
  
  .page-header {
    margin-bottom: 20px;
  }
  
  .page-title {
    font-size: 2.2rem;
  }
  
  .album-overlay {
    padding: 15px;
  }
  
  .album-title {
    font-size: 1.4rem;
  }
  
  .dialog-header {
    padding: 20px;
    flex-direction: column;
    gap: 15px;
  }
  
  .header-meta {
    justify-content: center;
  }
  
  .gallery-image {
    height: 150px;
  }
}

@media (max-width: 480px) {
  .album-title {
    font-size: 1.2rem;
  }
  
  .album-description {
    font-size: 0.9rem;
  }
  
  .gallery-image {
    height: 120px;
  }
}
</style>
